package com.hellobike.haxing.modules.product.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author ZLB
 * @Date 2020/1/21
 * @Version 1.0
 **/
public class MapUtil {

    /**
     * 将源map根据指定规则转换为目标map
     * @param sourceMap
     * @param keyConvertor
     * @param valueConvertor
     * @param <K>
     * @param <T>
     * @param <V>
     * @param <X>
     * @return
     */
    public static <K, T, V, X> Map<V, X> convert(Map<K, T> sourceMap, Convertor<K, V> keyConvertor, Convertor<T, X> valueConvertor) {
        Map<V, X> targetMap = new HashMap<>();
        sourceMap.forEach((K k, T t) -> targetMap.put(keyConvertor.map(k), valueConvertor.map(t)));
        return targetMap;
    }

    /**
     * 将map键值互换
     * @param map
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<V, K> reverse(Map<K, V> map) {
        Map<V, K> recerseMap = new HashMap<>();
        map.forEach((K k, V v) -> recerseMap.put(v, k));
        return recerseMap;
    }
}
